<html>

<head>
<title>P4COM - COM Interface to Perforce C++ API on Windows</title>

<style TYPE="text/css">
<!--

BODY {font: 12px/16px verdana, arial, sans-serif !important;
            text-align: left;
            margin-left: 2cm;
            color:#000000 !important;}

.cell {font: 12px/16px verdana, arial, sans-serif !important;
            text-align: left;
            color:#000000 !important;}

TD {font: 12px/16px verdana, arial, sans-serif !important;
            text-align: left;
            color:#000000 !important;}
            
H1 {font: 18px/18px verdana, arial, sans-serif !important;
            text-align: left;
            color:#333333 !important;}

H2 {font: 18px/18px verdana, arial, sans-serif !important;
            text-align: left;
            color:#333333 !important;
            font-weight: bold;
            margin-bottom: 14px}

H3 {font: 14px/14px verdana, arial, sans-serif !important;
            text-align: left;
            color:#333333 !important;
            font-weight: bold;
            margin-bottom: 14px}
pre {
    border-right: #646464 1px solid;
    padding-right: 0.5em;
    border-top: #646464 1px solid;
    padding-top: 0.5em;
    border-left: #646464 1px solid;
    padding-left: 0.5em;
    border-bottom: #646464 1px solid;
    padding-bottom: 0.5em;
    white-space: pre;
    background-color: #f6e6e6;
    color: black;
    margin-left: 1em;
    width: 90%;
    display: table;
    font-size: 100%;
}

-->
</style>

</head>

<body>

<h1><a name="top"></a>P4COM - COM Interface to Perforce C++ API on Windows</h1>
<p>p4com is contributed by Robert Cowham of Vaccaperna Systems Ltd. Robert is a
<a href="http://public.perforce.com/guest/robert_cowham/pcp.html">Perforce 
Consulting Partner and Certified Trainer</a> (contact me via that page).</p>

<p>I presented a paper on an earlier version at the Perforce User Conference in 2001:
<a href="http://www.perforce.com/perforce/conf2001/index.html#myth">Adventures 
in API Land</a> for an overview of the API and how I use it. In particular pay 
attention to the section on error handling!</p>

<p>This DLL  is part of
<a href="http://www.perforce.com/perforce/products/plugins-ofc.html">P4OFC - the 
integration between Microsoft Office and Perforce</a> and well tested and stable 
in that environment! You do not need to install another copy of this DLL if you 
already have P4OFC (2005.1 version) installed.</p>

<h3 ALIGN="LEFT">Example Projects</h3>
<p ALIGN="LEFT">These are to be found in the zip file and in the test 
subdirectory in the Public Depot.</p>

<p ALIGN="LEFT">They demonstrate how to read and set environment values 
(p4.port, p4.client), and also how to perform a submit using a combination of 
&quot;p4 change -o&quot;, manipulate the results and then execute the command with &quot;p4 
submit -i&quot;</p>

<p ALIGN="LEFT">There are several example projects (very similar functionality):</p>

<ul>
  <li>

<p ALIGN="LEFT">VB6</p>

  </li>
  <li>

<p ALIGN="LEFT">VB.NET</p>

  </li>
  <li>

<p ALIGN="LEFT">C# (.NET)</p>

  </li>
  <li>

<p ALIGN="LEFT">Delphi 7 (very rudimentary at this point)</p>

  </li>
</ul>

<p ALIGN="LEFT">There is also:</p>

  <ul>
    <li>

<p ALIGN="LEFT">VBScript example (windows scripting)</p>

    </li>
</ul>

<p ALIGN="LEFT">Just load them up, compile and run and you should be fine 
(provided DLL was previously registered).</p>

<p ALIGN="LEFT">Note that Nick Levine has now made available a Lisp interface 
(with LispWorks) package which uses P4COM - see:</p>

<ul>
  <li>

<p ALIGN="LEFT"><a href="http://www.nicklevine.org/p4com/">
www.nicklevine.org/p4com/</a> </p>

  </li>
</ul>

<h2>Installation/Download</h2>

<p>You can download the complete source and a 
<a href="http://public.perforce.com/guest/robert_cowham/perforce/API/p4com/main/src/releasemindependency/p4com.dll">pre-built (release) version</a> of the 
resulting DLL from <a href="p4com.zip">p4com.zip</a> or from the src directory 
in the Perforce Public Repository (<a href="ftp://public.perforce.com/guest/robert_cowham/perforce/API/p4com/main/">browse 
via ftp</a>).</p>
<p>To use it, you need to register the DLL (run regsvr32.exe on the DLL). I 
suggest you install in &quot;c:\program 
files\perforce&quot; or wherever you installed Perforce.</p>
<p>An installer will be provided in the future.</p>

<h2>Compiler</h2>
<p>This COM DLL has been built with MS VC++ 6.0 (SP5) and it also compiles happily with 
VC++ 7.0 (VS.NET).</p>
<p>Use either of the project settings:</p>
<ul>
  <li>Debug</li>
  <li>Release Min Dependency</li>
</ul>

<h2 ALIGN="LEFT">Main Interface</h2>
<p ALIGN="LEFT">The interface has been reworked from previous incarnations and 
is similar to that implemented by Tony Smith for Ruby.</p>
<p ALIGN="LEFT">There is a single object P4COM.p4 which should be 
instantiated and used.</p>
<p>Note that the command will pick up Perforce environment information in the 
the normal way - so global settings are used, or P4CONFIG settings are used. 
Make sure the current directory where your code is executing is set 
appropriately.</p>
<h2>Examples</h2>
<p>The simplest way to test is from VBScript. Note that since this can't access 
COM functions using String Arrays, it uses similar calls which take Variant 
Arrays.</p>
<pre>Test.vbs:
---------------
Option Explicit

Dim p4, arr, str, info, errs, warns

Set p4 = WScript.CreateObject(&quot;P4COM.p4&quot;)

p4.port = &quot;1666&quot;
p4.client = &quot;bruno_ws&quot;
p4.ExceptionLevel = 0
p4.connect

DisplayResult(&quot;info&quot;)
DisplayResult(&quot;describe 704&quot;)

p4.disconnect
p4.Tagged
p4.connect
DisplayResult(&quot;describe 704&quot;)

sub PrintArray(msg, arr)
    if (UBound(arr) >= 0) then
        WScript.Echo(msg & join(arr, chr(13) & chr(10)))
    end if
end sub

sub DisplayResult(cmd)
    WScript.Echo("Cmd: " & cmd)
    PrintArray "Info: ", p4.run_variant(cmd)
    PrintArray "Warnings: ", p4.Warnings_variant
    PrintArray "Errors: ", p4.Errors_variant
end sub
</pre>

<p>See other VBScript examples: <a href="test/test_parse.vbs">test\test_parse.vbs</a> 
and <a href="test/test_submit.vbs">test\test_submit.vbs</a>.</p>
<p>As an example of using it, in a VB project, make sure you have set a 
reference to COM object &quot;p4com 1.0 Type Library&quot;.</p>
<p>You can then execute a simple command such as &quot;p4 info&quot;:</p>
<pre>Dim p4 As P4COM.p4
Dim OutputArr() As String
Dim i as integer

p4.Connect
OutputArr = p4.run(&quot;info&quot;)
For i = LBound(OutputArr) To UBound(OutputArr)
    ResultBox.AddItem OutputArr(i)
Next
p4.Disconnect
</pre>

<p ALIGN="LEFT">This assumes a Listbox called ResultBox.</p>

<p ALIGN="LEFT">Something similar in C#:</p>
<pre>    P4COM.p4 m_p4 = new P4COM.p4();
    System.Array output;
    System.Array warnings;
    System.Array errors;
		
    m_p4.Connect();
    m_p4.ExceptionLevel = 1;
    try
    {
            output = m_p4.run(txtCmd.Text);
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
            MessageBox.Show(ex.Message);
    }
    DisplayResult();

    m_p4.Disconnect();
</pre>

<h3 ALIGN="LEFT">Using Forms</h3>

<p ALIGN="LEFT">It is also possible to do a Submit, getting and setting various 
parts of the submit form:</p>

<p ALIGN="LEFT">In C#:</p>

<pre>    public System.Windows.Forms.CheckedListBox Files;
    System.Array m_Files;
    m_p4.ParseForms();
    m_p4.Connect();
    output = m_p4.run("change -o");
    Description.Text = m_p4.get_Var("Description"); // Simple string variable
    m_Files = m_p4.get_ArrayVar("Files"); // StringArray variable
    Files.Items.Clear();
    for (i = m_Files.GetLowerBound(0); i <= m_Files.GetUpperBound(0); i++)
    {
        Files.Items.Add(m_Files.GetValue(i));
    }
</pre>

<p ALIGN="LEFT">The above gets values from the submit. Now we update having 
allowed the user to select files and type in a description, and finish 
the submit:</p>

<pre>    int i, count;
        
    m_p4.set_Var("Description", Description.Text); // Setting string var
    count = 0;
    for (i = 0; i < Files.Items.Count; i++)
    {
        if (Files.GetItemChecked(i))
        {
            count++;
        }
    }
    if (count > 0)
    {
        // There must be a better way of resetting m_Files than this
        // but I haven't found it yet!!
        string[] arr = new string[count];
        int j = 0;
        foreach(object item in Files.CheckedItems) 
        {
            arr[j++] = item.ToString();
        }
        m_Files = arr;	// Resets the array to new size
        m_p4.set_ArrayVar("Files", ref m_Files);
        output = m_p4.run("submit -i");
        errors = m_p4.Errors;

        string s = join(output);	// join output array together into one string
        s = s + "\n" + "Errors: " + join(errors);
        MessageBox.Show(s);
    }
</pre>

<h2>Unicode Enabled</h2>
<p>p4com.dll is unicode enabled and is an example of how to use the Perforce API 
with Unicode servers (where P4CHARSET is in use). Being COM, all strings such as 
filenames, change descriptions and the like, may be in Unicode and will be 
handled correctly - the challenge then is to be able to display the results!</p>

<h2>Interface</h2>
<h3>Properties</h3>
<p>The following properties may be read or updated. If you are updating them, 
then this must be done prior to calling Connect. If these properties are not 
set, then they default to system global settings (e.g. registry or environment, 
or P4CONFIG settings depending on the current directory).</p>
<ul>
  <li>Charset - equivalent of P4CHARSET</li>
  <li>Client - equivalent of P4CLIENT</li>
  <li>Cwd - Current working directory</li>
  <li>Host - Equivalent of P4HOST</li>
  <li>Language - Equivalent of P4LANGUAGE</li>
  <li>Os - Current operating system (read-only)</li>
  <li>Password - Equivalent of P4PASSWD</li>
  <li>Port - Equivalent of P4PORT</li>
  <li>User - Equivalent of P4USER</li>
</ul>
<h3>Running commands</h3>
<ul>
  <li>Connect - Connect to Perforce server - generates exception if fails.</li>
  <li>Disconnect - Disconnect from server</li>
	<li>Dropped - Has the connection dropped or not?<br>
&nbsp;</li>
  <li>run(cmd) - Returns the results of running the Perforce command as array of 
  strings (see example above)</li>
</ul>
<blockquote>

<p>cmd is a simple (valid) perforce command (with quotes around embedded spaces 
and without leading &quot;p4&quot;), such as:</p>
  <blockquote>

<p>info<br>
edit //depot/main/jam.c<br>
edit -t text+w &quot;//depot/some path with spaces/file.c&quot;<br>
clients</p>
  </blockquote>
</blockquote>
<ul>
  <li>run_variant - does the same as run but for VBScript or other environments 
	which require a variant to be returned</li>
	<li>Input - property to provide input, e.g. for setting password prior to 
	running login command.</li>
	<li>Errors - Returns array of any Errors that occurred during previous 'run'</li>
	<li>Errors_variant - same as Errors but for VBScript</li>
  <li>Warnings - Returns array of any Warnings that occurred during previous 
  'run'</li>
	<li>Warnings_variant - same as Warnings but for VBScript</li>
  <li>ExceptionLevel - Property to get and set this value: 0 means no 
  exceptions, 1 exception on errors only, 2 exceptions on warnings as well</li>
  <li>Tagged - Set's tagging of results like &quot;p4 -ztag&quot; global option</li>
  <li>ParseForms - Turns on Form parsing. Must be executed prior to Connect. 
  <b>This allows the use of Var and VarArray to get and set values. </b>Is only 
  relevant for commands which take the -o/-i flags such as p4 
  user/change/submit/jobspec/etc.</li>
  <li>TempFilename - Read-only property which is the name of any temporary file 
  produced by a p4 command such as 'print'</li>
  <li>Var(varName) - Get the value of the variable with the specified name 
  (requires ParseForms to have been set prior to Connect)</li>
  <li>VarExists(varName) - Does variable with specified name exist or not?</li>
  <li>ArrayVar(varName) - Get or set the array of values of the variable with 
  the specified name (requires ParseForms)</li>
  <li>ArrayVar_variant(varName) - Same as ArrayVar but returns variant for 
	VBScript etc</li>
	<li>FormatDateTime - Utility function for VB to format Unix date/time 
	seconds value as string using C Runtime Library call</li>
</ul>

<h3>Credits</h3>
<p>Thanks to Tony Smith who helped along the way, and whose
<a href="http://public.perforce.com/guest/tony_smith/perforce/API/Ruby/main/doc/index.html">
Ruby API</a> provided inspiration.</p>

<h3>Changes</h3>

<p>2005-11-10&nbsp;&nbsp;&nbsp; Changes (2005.1.0.0)</p>
<blockquote>
	<blockquote>
		<ul>
			<li>Added Dropped() to detect dropped connections</li>
			<li>Added Input property to provide input e.g. for passwords</li>
			<li>Added FormatDateTime() as a convenience for calling C Runtime 
			function (for VB use)</li>
			<li>Updated to use 2005.1 P4API</li>
		</ul>
	</blockquote>
</blockquote>
<p>2005-01-05&nbsp;&nbsp;&nbsp; New version (2004.2.0.1):</p>
<blockquote>
	<blockquote>
		<ul>
			<li>reworked to reduce external dependencies.</li>
			<li>Allow you to set ArrayVar_variants as well as get them.</li>
		</ul>
	</blockquote>
</blockquote>
<p>2004-03-23&nbsp;&nbsp;&nbsp; Changes:</p>

<blockquote>
  <blockquote>
    <ul>
      <li>Fixed some String leaks. </li>
      <li>Also problem with writing to temporary files. </li>
      <li>Updated p4API to released version of 2003.2.</li>
      <li>Start of Delphi example.</li>
    </ul>
  </blockquote>
</blockquote>

<p>2004-02-06&nbsp;&nbsp;&nbsp; Returns the output of diff command in normal 
output array.</p>

<p>2004-01-28&nbsp;&nbsp;&nbsp; Now able to be used from VBScript.</p>

<p>2003-11-07&nbsp;&nbsp;&nbsp; Added reference to Lisp usage of P4Com.</p>

<p>2003-09-02&nbsp;&nbsp;&nbsp; Tidied up. Added ArrayVarVar put property to 
allow Variant arrays to be passed in (for Lisp compatibility!).</p>

<p>2003-08-08&nbsp;&nbsp;&nbsp; Rename P4COMLib to P4COM and made run a method 
rather than property.</p>

<p>
<a href="#top">Top</a>
</p>
</body>

</html>